<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\BuildingAction;
use App\Admin\Repositories\Building;
use App\library\services\BuildingService;
use App\library\services\FangAttrsService;
use App\Models\Cities;
use App\Models\FangAttrs;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use QL\QueryList;

class BuildingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Building(), function (Grid $grid) {

            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('tags');
            $grid->column('address');
            $grid->column('type');
            $grid->column('area');
            $grid->column('avg_price');
            $grid->column('total_price');
            $grid->column('image')->image();
            $grid->column('province', '省份')->display(function ($res) {
                return Cities::find($res)->name;
            });
            $grid->column('city', '城市')->display(function ($res) {
                return Cities::find($res)->name;
            });;
            $grid->column('region', '县区')->display(function ($res) {
                return Cities::find($res)->name;
            });;
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append(new BuildingAction());
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('province', '省份')->select(Cities::where('pid',0)->pluck('name', 'id'))
                    ->load('city', 'city')->width(3);
                $filter->equal('city', '城市')->select(Cities::where('pid','<>',0)->pluck('name', 'id'))
                    ->load('region', 'region')->width(3);
                $filter->equal('region', '区')->select(function ($id) {
                    return Cities::options($id);
                })->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Building(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('tags');
            $show->field('address');
            $show->field('type');
            $show->field('area');
            $show->field('avg_price');
            $show->field('total_price');
            $show->field('image');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Building(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->text('tags');
            $form->text('address');
            $form->text('type');
            $form->text('area');
            $form->text('avg_price');
            $form->text('total_price');
            $form->image('image');
            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    /**
     * Author:YangXinPeng
     * 楼盘信息采集
     * @return mixed
     */
    public function resources()
    {
        $instance = new BuildingService();
        $res = $instance->catchResource();
        return $res;
    }


    public function types(Request $request)
    {
        $buildingId = $request->get('q');
        $ids = [];
        $data = \App\Models\Building::where('id', $buildingId)->first()->toArray();
        $arr = explode(",", $data['type']);
        $types = FangAttrsService::getHouseType();
        foreach ($types as $value) {
            $num = $value['name'][0];
            if (in_array($num, $arr)) {
                $ids [] = $value['id'];
            }
        }
        return FangAttrs::whereIn('id', $ids)->get(['id', 'name as text']);
    }
}
